package atguigu.forkjoin;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 异步任务，把耗时的任务放在未来，等主线工作完成，然后获取未来任务的结果
 * @author ex_yejing1
 *
 */
public class Demo01 {

	public static void main(String[] args) throws ExecutionException, InterruptedException {
		// 没有返回值的 runAsync() 异步回调
		CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
			try {
				System.out.println("模拟一个很耗时的任务...");
				TimeUnit.SECONDS.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "runAsync=>void");
		});

		System.out.println("主线任务继续进行...");
		TimeUnit.SECONDS.sleep(5);
		completableFuture.get(); // 获取阻塞执行结果
		System.out.println("获取结果后，程序结束");
		
	}
}